//
//  THAddressHomeViewController.m
//  TreeHole
//
//  Created by apple on 2020/9/1.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THAddressHomeViewController.h"
#import "THAddressHomeTableViewCell.h"
#import "THAddAddressViewController.h"
@interface THAddressHomeViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;/**<  */

@end

@implementation THAddressHomeViewController
- (void)dealloc{
    if (self.dataSouce.count == 0) {
        if (self.didSlectedAddressModel) {
            self.didSlectedAddressModel(nil);
        }
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的地址";
    __weak typeof(self) weakSelf =  self;
    self.tableView.st_noreslutView = [[STTools_NoresultView alloc] initWithFrame:CGRectMake(0, self.tableView.tableHeaderView.height + 30, STONEOBS_UI_UISCREEN_WIDTH, 400) title:@"暂无数据" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 1;
        [weakSelf stoneobs_fun_st_send_list_request];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    [self stoneobs_fun_st_setRightItemWithTitle:@"添加" titleColor:STONEOBS_UI_FIRST_TEXT_COLOR];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.page = 1;
    [self stoneobs_fun_st_send_list_request];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return THAddressHomeTableViewCell.stoneobs_ui_cell_height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    THAddressHomeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[THAddressHomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    THAddressModel * model = self.dataSouce[indexPath.row];
    cell.model = model;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    THAddressModel * model = self.dataSouce[indexPath.row];
    if (self.didSlectedAddressModel) {
        self.didSlectedAddressModel(model);
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        THAddAddressViewController * vc = THAddAddressViewController.new;
        vc.addressModel = model;
        [self pushViewController:vc animated:1];
    }

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)stoneobs_fun_st_rightBarAction:(id)sender{
    [self pushViewController:THAddAddressViewController.new animated:1];
}
#pragma mark --stoneobs_net netWork Method
- (void)stoneobs_fun_st_send_list_request{
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    [stoneobs_net_param_dic setObject:@(self.page) forKey:@"page"];
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/user/addresses"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumGet
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        NSArray * stoneobs_net_array = [responseObject valueForKey:@"data"];
        NSMutableArray * dataSouce = [THAddressModel mj_objectArrayWithKeyValuesArray:stoneobs_net_array];
        self.dataSouce = dataSouce;
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        [SVProgressHUD dismiss];
        [self.tableView.mj_header endRefreshing];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}

@end
